class BookController < ApplicationController

  before_filter :verify_session
  def index
    list
    render_action 'list'
  end
  
  def list
    @books_pages, @books = paginate :books, :per_page => 10
  end
  
  def new
    new_init
    @book = Book.new
    @book.book_status_id = 1;
  end
  
  def create
    @book = Book.new(params[:book])
    
    if @book.save
      flash[:info]="添加成功！"
      redirect_to :action => 'list'
    else
      flash[:info]="添加失败!"
      new_init
      render :action => 'new'
    end
  end
  
  def destroy
    Book.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
  
  def edit
    @publishes = getAllPublishes
    @book = findById(params[:id])
  end
  
  def update
    book =  findById(params[:id])
    if book.update_attributes(params[:book])
      flash[:info]="保存成功!"
      redirect_to :action => 'list'
      else
      flash[:info]="保存失败!"
      redirect_to :action => 'edit'
    end
  end
  
  def show 
    @publishes = getAllPublishes
    @book = findById(params[:id])
#    @out_record = OutRecord.find(:first,:conditions => ["book_id = ?",params[:id]])
    render :layout => false
  end
  
  private 
  def getAllPublishes
      Publish.find(:all)
  end
  
  def findById(id)
    Book.find(id)
  end
  
  def new_init
    @publishes = getAllPublishes
  end
end
